package com.jdn;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.util.Scanner;

/**
 * User: arsentyev
 * Date: 12.09.12
 */
public class MainClass {
    public static void main(String... args) {
        final String mainDir = "D:\\dev\\docs\\java\\";
        final String htmlDir = "D:\\dev\\docs\\html\\";

        final File main = new File(mainDir);
        final FileFilter ff = new FileFilter() {
            public boolean accept(File file) {
                return file.isDirectory();
            }
        };

        final FilenameFilter fnf = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(".java");
            }
        };

        for(final File file: main.listFiles(ff)) {
            new Thread(new Runnable() {
                public void run() {
                    for(File dr: file.listFiles(ff)) {
                        files(dr);
                    }
                }

                public void files(File dir) {
                    for(File java: dir.listFiles(fnf)) {
//                        System.out.println(java.getAbsolutePath().replace(mainDir, "").replace(File.separator, "."));
                        Container container = null;
                        try {
                            String name = java.getAbsolutePath().replace(mainDir, "").replace(File.separator, ".");
                            name = name.substring(0, name.length() - 5);
                            container = Parser.parse(new Scanner(new FileInputStream(java), "UTF-8").useDelimiter("\\A").next(), name);
                            if(container != null) {
                                String html = HtmlBuilder.buildHtml(container);
                                FileUtils.writeToFile(htmlDir + container.getPathName() + ".html", html);
                            }
                        } catch (FileNotFoundException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }).start();
        }
    }
}